package flex.cc.vehicle_svr.service.impl;

import flex.cc.PublicUtil;
import flex.cc.common.utils.TypeConverUtils;
import flex.cc.order.manage.dao.*;
import flex.cc.order.manage.dto.order.*;
import flex.cc.system.entity.DictCodeEntity;
import flex.cc.system.service.DictCodeService;
import flex.cc.vehicle_svr.service.AdditionalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author wkangle
 * @className AdditionalerviceImpl
 * @description 补充接口
 * @time 2021-09-06 18:50:05
 */
@Service("additionalService")
@Transactional(rollbackFor = Exception.class)
public class AdditionalerviceImpl implements AdditionalService {
    private static final Logger logger = LoggerFactory.getLogger(AdditionalerviceImpl.class);

    @Autowired
    private OrderMainDao orderMainDao;
    @Autowired
    private OrderPolicyDao orderPolicyDao;
    @Autowired
    protected OrderRescueDao orderRescueDao;
    @Autowired
    private OrderCustomerDao orderCustomerDao;
    @Autowired
    private OrderTrackDao orderTrackDao;
    @Autowired
    private DictCodeService dictCodeService;

    @Value("${serviceprovider.pengda.serverId}")
    private String serverId;

    @Value("${serviceprovider.pengda.serverName}")
    private String serverName;

    /**
     * @description 上游补充接口接口相关业务处理
     * @param requestBodyMap 补充接口请求体
     * @return
     * @author wkangle
     * @time 2021/09/08 0008 9:58
     * @exception/throws
    */
    @Override
    public String additional(Map<String, Object> requestBodyMap) {
        if (PublicUtil.isNotEmpty(requestBodyMap.get("ownerOrderNo"))) {
            String ownerOrderNo = requestBodyMap.get("ownerOrderNo").toString();
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("ownerOrderNo", ownerOrderNo);
            OrderMainEntity orderMainEntity = orderMainDao.getOneByParams(params);
            if (orderMainEntity != null) {
                String orderNo = orderMainEntity.getOrderNo();
                Date currentDate = new Date();
                DictCodeEntity dictCodeEntity = dictCodeService.searchAllCodeByTypeCode("sourceName", "01", 0, null); // 取消接口的操作人是渠道商名称 TODO 默认渠道名称是电话中心-01
                String sourceName01 = dictCodeEntity.getValue();

                // 服务单主表
                orderMainEntity.setOwnerOrderNo(ownerOrderNo);
                orderMainEntity.setOwnerSubOrderNo(PublicUtil.isNotEmpty(requestBodyMap.get("ownerSubOrderNo")) ? requestBodyMap.get("ownerSubOrderNo").toString() : null);
                orderMainEntity.setOperatorId(PublicUtil.isNotEmpty(requestBodyMap.get("operatorId")) ? requestBodyMap.get("operatorId").toString() : null);
                String incidentType = PublicUtil.isNotEmpty(requestBodyMap.get("incidentType")) ? requestBodyMap.get("incidentType").toString() : null;
                orderMainEntity.setIncidentType(PublicUtil.isNotEmpty(incidentType) ? ("0".equals(incidentType) ? 0 : "1".equals(incidentType) ? 1 : null) : null);
                // 转译服务类型
                String serviceType = PublicUtil.isNotEmpty(requestBodyMap.get("serviceType")) ? requestBodyMap.get("serviceType").toString() : null;
                serviceType = TypeConverUtils.getServiceType("01", serviceType);
                requestBodyMap.put("serviceType", serviceType);
                orderMainEntity.setServiceItem(serviceType);
                orderMainEntity.setBranch(PublicUtil.isNotEmpty(requestBodyMap.get("branch")) ? requestBodyMap.get("branch").toString() : null);
                orderMainEntity.setBranchName(PublicUtil.isNotEmpty(requestBodyMap.get("branchName")) ? requestBodyMap.get("branchName").toString() : null);
                orderMainEntity.setProvince(PublicUtil.isNotEmpty(requestBodyMap.get("province")) ? requestBodyMap.get("province").toString() : null);
                orderMainEntity.setCity(PublicUtil.isNotEmpty(requestBodyMap.get("city")) ? requestBodyMap.get("city").toString() : null);
                orderMainEntity.setDistrict(PublicUtil.isNotEmpty(requestBodyMap.get("district")) ? requestBodyMap.get("district").toString() : null);
                orderMainEntity.setCheckerName(PublicUtil.isNotEmpty(requestBodyMap.get("checkerName")) ? requestBodyMap.get("checkerName").toString() : null);
                orderMainEntity.setCheckerPhone(PublicUtil.isNotEmpty(requestBodyMap.get("checkerPhone")) ? requestBodyMap.get("checkerPhone").toString() : null);

                orderMainEntity.setUpdateTime(currentDate);
                orderMainEntity.setUpdater(sourceName01);
                orderMainDao.update(orderMainEntity);

                HashMap<String, Object> info = new HashMap<>();
                info.put("orderNo", orderNo);
                // 服务单保单信息表
                OrderPolicyEntity orderPolicyEntity = orderPolicyDao.getOneByParams(info);
                orderPolicyEntity.setOrderNo(orderNo);
                orderPolicyEntity.setInsured(PublicUtil.isNotEmpty(requestBodyMap.get("insuredName")) ? requestBodyMap.get("insuredName").toString() : null);
                orderPolicyEntity.setCarNo(PublicUtil.isNotEmpty(requestBodyMap.get("vehicleNo")) ? requestBodyMap.get("vehicleNo").toString() : null);
                orderPolicyEntity.setCarModel(PublicUtil.isNotEmpty(requestBodyMap.get("vehicleType")) ? requestBodyMap.get("vehicleType").toString() : null);
                orderPolicyEntity.setCarVin(PublicUtil.isNotEmpty(requestBodyMap.get("vin")) ? requestBodyMap.get("vin").toString() : null);
                orderPolicyEntity.setPolicyNumber(PublicUtil.isNotEmpty(requestBodyMap.get("policyNumber")) ? requestBodyMap.get("policyNumber").toString() : null);

                orderPolicyEntity.setUpdateTime(currentDate);
                orderPolicyEntity.setUpdater(sourceName01);
                orderPolicyDao.update(orderPolicyEntity);

                // 服务单客户信息表
                OrderCustomerEntity orderCustomerEntity = orderCustomerDao.getOneByParams(info);
                orderCustomerEntity.setUsername(PublicUtil.isNotEmpty(requestBodyMap.get("reporterName")) ? requestBodyMap.get("reporterName").toString() : null);
                orderCustomerEntity.setMobile(PublicUtil.isNotEmpty(requestBodyMap.get("reporterPhone")) ? requestBodyMap.get("reporterPhone").toString() : null);
                orderCustomerEntity.setIdCard(PublicUtil.isNotEmpty(requestBodyMap.get("reporterIdNo")) ? requestBodyMap.get("reporterIdNo").toString() : null);

                orderCustomerEntity.setUpdateTime(currentDate);
                orderCustomerEntity.setUpdater(sourceName01);
                orderCustomerDao.update(orderCustomerEntity);

                // 服务单待救援信息
                OrderRescueEntity orderRescueEntity = orderRescueDao.getOneByParams(info);
                orderRescueEntity.setAddrAddress(PublicUtil.isNotEmpty(requestBodyMap.get("caseLocation")) ? requestBodyMap.get("caseLocation").toString() : null);
                orderRescueEntity.setEnv(PublicUtil.isNotEmpty(requestBodyMap.get("dragEnvironment")) ? requestBodyMap.get("dragEnvironment").toString() : null);
                orderRescueEntity.setAddrProvince(PublicUtil.isNotEmpty(requestBodyMap.get("caseProvince")) ? requestBodyMap.get("caseProvince").toString() : null);
                orderRescueEntity.setAddrCity(PublicUtil.isNotEmpty(requestBodyMap.get("caseCity")) ? requestBodyMap.get("caseCity").toString() : null);
                orderRescueEntity.setAddrCounty(PublicUtil.isNotEmpty(requestBodyMap.get("caseCounty")) ? requestBodyMap.get("caseCounty").toString() : null);
                Double caseLongitude = PublicUtil.isNotEmpty(requestBodyMap.get("caseLongitude")) ? Double.valueOf(requestBodyMap.get("caseLongitude").toString()) : null;
                orderRescueEntity.setAddrLongitude(caseLongitude);
                Double caseLatitude = PublicUtil.isNotEmpty(requestBodyMap.get("caseLatitude")) ? Double.valueOf(requestBodyMap.get("caseLatitude").toString()) : null;
                orderRescueEntity.setAddrLatitude(caseLatitude);
                orderRescueEntity.setDesAddress(PublicUtil.isNotEmpty(requestBodyMap.get("desAddress")) ? requestBodyMap.get("desAddress").toString() : null);
                orderRescueEntity.setDesProvince(PublicUtil.isNotEmpty(requestBodyMap.get("desState")) ? requestBodyMap.get("desState").toString() : null);
                orderRescueEntity.setDesCity(PublicUtil.isNotEmpty(requestBodyMap.get("desCity")) ? requestBodyMap.get("desCity").toString() : null);
                orderRescueEntity.setDesCounty(PublicUtil.isNotEmpty(requestBodyMap.get("desCounty")) ? requestBodyMap.get("desCounty").toString() : null);
                Double desLongitude = PublicUtil.isNotEmpty(requestBodyMap.get("desLongitude")) ? Double.valueOf(requestBodyMap.get("desLongitude").toString()) : null;
                orderRescueEntity.setDesLongitude(desLongitude);
                Double desLatitude = PublicUtil.isNotEmpty(requestBodyMap.get("desLatitude")) ? Double.valueOf(requestBodyMap.get("desLatitude").toString()) : null;
                orderRescueEntity.setDesLatitude(desLatitude);

                orderRescueEntity.setUpdateTime(currentDate);
                orderRescueEntity.setUpdater(sourceName01);
                orderRescueDao.update(orderRescueEntity);

                // 服务单轨迹表
                OrderTrackEntity orderTrackEntity = new OrderTrackEntity();
                orderTrackEntity.setDataId(UUID.randomUUID().toString().replace("-", ""));
                orderTrackEntity.setOrderNo(orderNo);
                orderTrackEntity.setTrackType("00"); // 补充接口备注 TODO
                orderTrackEntity.setTrackDesc("补充接口：" + (PublicUtil.isNotEmpty(requestBodyMap.get("serviceContent")) ? requestBodyMap.get("serviceContent").toString() : null));
                orderTrackEntity.setStatus(orderMainEntity.getStatus()); // 轨迹状态与服务单主表状态保持一致

                orderTrackEntity.setIsDelete(0);
                orderTrackEntity.setCreateTime(currentDate);
                orderTrackEntity.setCreater(sourceName01);
                orderTrackEntity.setUpdateTime(currentDate);
                orderTrackEntity.setUpdater(sourceName01);
                orderTrackDao.insert(orderTrackEntity);

                logger.info("additional requestBodyMap--> " + requestBodyMap);

                return "00";
            } else {
                return "02";
            }
        } else {
            return "01";
        }
    }

}
